package cn.simon.free.redis.jedis.hash;

import cn.simon.free.redis.jedis.common.AbstractJedis;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * redis - hash
 *
 * @author simon
 * @date 2021/5/14 10:34
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
@Component
public class JedisHashSupport extends AbstractJedis implements JedisHash {

    @Override
    public void hmSet(String key, String field, String value) {
        super.redisTemplate.opsForHash().put(key, field, value);
    }

    @Override
    public Object hmGet(String key, String field) {
        return super.redisTemplate.opsForHash().get(key, field);
    }

    @Override
    public List<Object> hmGet(String key, String... field) {
        return super.redisTemplate.opsForHash().multiGet(key, Arrays.asList(field));
    }

    @Override
    public void hmSet(String key, Map<Object, Object> fieldWithValue) {
        super.redisTemplate.opsForHash().putAll(key, fieldWithValue);
    }

    @Override
    public Map<Object, Object> hmGetAll(String key) {
        return super.redisTemplate.opsForHash().entries(key);
    }

    @Override
    public Long hmDel(String key, String... fields) {
        return super.redisTemplate.opsForHash().delete(key, fields);
    }

    @Override
    public Boolean hmExist(String key, String field) {
        return super.redisTemplate.opsForHash().hasKey(key, field);
    }

    public Boolean setNxWithExpire(String key, Object field, Object value, long timeOut, TimeUnit timeUnit) {
        Boolean f = super.redisTemplate.opsForHash().putIfAbsent(key, field, value);
        return f && super.setExpire(key, timeOut, timeUnit);
    }
}
